Version 2.0 of the Script Manager introduced a cool set of routines for formatting numeric values. Given a format specification, these routines can format a floating-point number into a string, or read a floating-point number from a string. But since this is a Macintosh, the format itself must be able to handle different languages and different ways of writing numbers. So Apple invented “canonical formats,” formats that are “compiled” into a script-independent representation. If you want to edit the canonical format, you can “decompile” it back into human-readable text (called a “number format specification string”) which makes sense to the current script system and user.
All this is great, but there’s a missing piece. There’s no easy way for a developer to create a canonical format for later use in an application. That’s why I wrote this FMAT editor. It’s a ResEdit editor that lets you edit 'FMAT' resources—canonical formats.
RESTRICTIONS
FMAT Editor requires Script Manager 2.0 or later and ResEdit 2.1 or later.
INSTALLATION
Installation is manual. Open your “ResEdit Preferences” file with ResEdit. It should be in your System Folder. Next open the file “FMAT Editor” and select all resource types (command-A) except 'vers' (command-click 'vers'). Copy and paste them into “ResEdit Preferences.” Save your changes and you’re done!
EDITING
Open the file “Sample FMATs.” This file contains some formats for displaying bytes, kilobytes, and megabytes of memory or disk space; percentages; and currency in U.S. dollars.
Open an 'FMAT' resource and you are presented with a dialog in which you can edit the human-readable number format specification string. Use tab or shift-tab to select the positive, negative, or zero portions of the format. Below the edit box are example values 1234.56, -1234.56, and zero, as they appear when this format is used. If a formatting error occurs, you see a message there instead. If the error is severe enough to prevent the format from compiling, the message appears in bold red text, and you cannot close the dialog until you correct the problem.
Beside the editing dialog is a floating palette of “number parts.” These are the tokens used by the format compiler to distinguish format elements in the current script on your computer. There are left- and right-quotes for enclosing literal strings; plus and minus signs; digits with various representations for leading zeroes (blank leading, zero leading, and suppressed leading); a thousands separator; a decimal point; a percentage symbol; an escape character (used before the other tokens to place them in a literal string); a separator for dividing the format into positive, negative, and zero parts; positive and negative exponent signs; and various literals that don’t require quotes. Simply click the buttons to type the symbols into the format.
If you change the current script while editing a format (using the Keyboard menu or command-space), FMAT Editor switches to the new script’s system font and starts using its number parts.
CODE
There are two THINK C source files, “FormatVal.c” and “FormatVal.h,” which contain the definition of a function FormatVal that formats a double into a string given an 'FMAT' resource ID. Feel free to adapt this code to your needs.
REFERENCE
You definitely need to get your hands on the new “Inside Macintosh: Text” from Addison-Wesley (ISBN 0-201-63298-5). There’s a nice section starting on p. 5-35 called “Working With Numeric Strings” that explains how numeric formatting works and how to build a number format specification string.
FUNKY STUFF
The Script Manager is apparently not bug-free. FormatRecToString (née, Format2Str) has problems with double-byte characters in a literal. This shows up when you edit a format that was intended for a 2-byte script system. For example, with the Japanese script system installed, create a new format, switch to the Japanese keyboard, click the open quote button, and type “nihonn.” Press the space bar to get Kanji and press return to enter the characters. Click the close quote button and you have a format. Looks great! Now close the editor and reopen it. Looks like garbage! The format is perfectly useable, so long as you only edit it once.
WHAT’S NEW IN 1.0.1?
• Resize the number parts palette when the keyboard script is changed, to accomodate different font sizes and a different number of unquoted literals.
• Update the examples when the keyboard script is changed, even if the format contains an error.
• Support Cut/Copy/Paste/Clear commands for edit text field.
YOURS TRULY
I hope you enjoy using FMAT Editor. If you have any comments, suggestions, or bug reports, e-mail them to me at Michael_Hecht@mac.sas.com.